package common.messages;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author zhou, shi, huang
 * 
 */
public class MD5Hash {

	/**
	 * Using MD5 method to hash the input
	 * 
	 * @param value
	 * @return hash value of input
	 */
	public String doHash(String value) {
		MessageDigest md;
		StringBuffer buf = new StringBuffer("");
		try {
			md = MessageDigest.getInstance("MD5");
			md.update(value.getBytes());
			byte b[] = md.digest();
			int i;
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
		} catch (NoSuchAlgorithmException e) {
			System.out.println("Hash Failed");
		}
		return buf.toString();
	}
}
